<template>
    <div style="width:100%;height:100%;overflow:hidden auto">
        <weui-input :list="input_0_list" title="" :show_more="false" :tips="null" agreement_name="" more_label="">
            <weui-button slot="button" type="primary" :is_disable="!input_0_list[0].value" @on-click="on_submit_button_click">
                {{isNew ? '添加' : '修改'}}
            </weui-button>
        </weui-input>
        <weui-cell :list="cell_0_list">
        </weui-cell>
    </div>
</template>
<script>
import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
import WeuiButton from '../vue2componentSet/src/components/WeuiButton'
import WeuiCell from '../vue2componentSet/src/components/WeuiCell'
import global from '../lib/global'
import axios from 'axios'

export default {
    components: {
        WeuiInput,
        WeuiButton,
        WeuiCell
    },
    data() {
        return {
            input_0_list: [{
                label: '手机号码',
                value: '',
                type: 'tel'
            }, {
                label: '名字',
                value: ''
            }],
            isNew: true,
            promotees: []
        }
    },
    mounted() {
        if (!this.$store.state.admin) {
            this.$router.goback()
            return
        }

        location.hash.split('?')[1] && location.hash.split('?')[1].split('&').forEach(item => {
            if (item.split('=')[0] == 'phone') {
                this.$set(this.input_0_list[0], 'value', item.split('=')[1])
                this.$set(this.input_0_list[0], 'readonly', true)
                this.isNew = false
            } else if (item.split('=')[0] == 'name') {
                this.$set(this.input_0_list[1], 'value', decodeURIComponent(item.split('=')[1]))
            }
        })

        this.$store.commit('set_title', this.isNew ? '添加推荐人' : '修改推荐人')

        this.getPromoterPromotee()
    },
    methods: {
        on_submit_button_click() {
            axios.post(global.server + 'setCustomer.do', {
                phone: this.input_0_list[0].value,
                name: this.input_0_list[1].value,
                isPromoter: true
            }).then(result => {
                var obj = result.data
                if (obj.result == 1)
                    this.$router.goback()
            })
        },
        getPromoterPromotee() {
            axios.post(global.server + 'getPromoterPromotee.do', {
                phone: this.input_0_list[0].value
            }).then(result => {
                var obj = result.data
                for (var i = 0; i < obj.length; ++i) {
                    for (var j = i + 1; j < obj.length; ++j) {
                        if (obj[i].returnPromoterMoney < obj[j].returnPromoterMoney ||
                            (obj[i].returnPromoterMoney == obj[j].returnPromoterMoney && obj[i].willReturnPromoterMoney < obj[j].willReturnPromoterMoney)
                        ) {
                            var tmp = obj[i]
                            obj[i] = obj[j]
                            obj[j] = tmp
                        }
                    }
                }
                this.promotees = obj
            })
        }
    },
    computed: {
        cell_0_list() {
            var result = []
            this.promotees.forEach(item => {
                result.push({
                    value: item.phone,
                    desc: '已返' + item.returnPromoterMoney + '(待返' + '' + item.willReturnPromoterMoney + '/待确认' + item.confirmingReturnPromoterMoney + ')',
                })
            })
            return result
        }
    }
}
</script>